package User;


import book.Library;
import utils.PermissionException;
import static Constant.constant.*;


public class ProxyUser {
    private User realUser;
    public ProxyUser(User user) {
        this.realUser = user;
    }

    public User getRealUser() {
        return realUser;
    }
    public int mune() {
        return this.realUser.mune();
    }
    //查看是否为管理员操作
    private void checkRightsAdmin(String exceptionMessage) {
        if(!(realUser instanceof AdminUser)) {
            throw new PermissionException(exceptionMessage);
        }
    }
    //查看是否为普通用户操作
    private void checkRightsNormal(String exceptionMessage) {
        if(!(realUser instanceof NormalUser)) {
            throw new PermissionException(exceptionMessage);
        }
    }
    //-----------------管理员操作----------------------
    public void addBook() {
        checkRightsAdmin("普通用户没有权限上架书籍！");
        ((AdminUser)realUser).addBook();
    }
    public void updateBook() {
        checkRightsAdmin("普通用户没有权限修改书籍！");
        ((AdminUser)realUser).updateBook();
    }
    //删除书籍
    public void removeBook() {
        checkRightsAdmin("普通用户没有权限查看每本书的借阅次数！");
        ((AdminUser)realUser).removeBook();
    }
    //统计每本书的借阅次数
    public void borrowCount() {
        checkRightsAdmin("普通用户没有权限查看每本书的借阅次数！");
        ((AdminUser)realUser).borrowCount();
    }
    //查询最受欢迎的前n本书
    public void mostlikeBook() {
        checkRightsAdmin("普通用户没有权限查询最受欢迎的前n本书！");
        ((AdminUser)realUser).mostlikeBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        checkRightsAdmin("普通用户没有权限查看库存状态！");
        ((AdminUser)realUser).checkInventoryStatus();
    }
    //并移除上架超过⼀年的图书
    public void checkAndRemoveOldBook() {
        checkRightsAdmin("普通用户没有权限移除上架超过⼀年的图书！");
        ((AdminUser)realUser).checkAndRemoveOldBook();
    }
    //-----------------普通用户操作----------------------
    //借阅图书
    public void borrowBook() {
        checkRightsNormal("请使用普通用户来借阅书籍");
        ((NormalUser)realUser).borrowBook();
    }
    //归还图书
    public void returnBook() {
        checkRightsNormal("请使用普通用户来归还书籍");
        ((NormalUser)realUser).returnBook();
    }
    //查看个人借阅情况
    public void viewBorrowBook() {
        checkRightsNormal("请使用普通用户来查看借阅情况");
        ((NormalUser)realUser).viewBorrowBook();
    }

    public void handleOperation(int choice) {
        if(realUser instanceof AdminUser) {
            switch (choice){
                case SEARCH_BOOK:
                    Library.getInstance().searchBook();
                    break;
                case DISPLAY_BOOK:
                    Library.getInstance().displayBook();
                    break;
                case EXIT:
                    Library.getInstance().exitBookSystem();
                    break;
                case ADD_BOOK:
                    addBook();
                    break;
                case UPDATE_BOOK:
                    updateBook();
                    break;
                case REMOVE_BOOK:
                    removeBook();
                    break;
                case BORROW_BOOK_COUNT:
                    borrowCount();
                    break;
                case MOST_LIKE_BOOK:
                    mostlikeBook();
                    break;
                case CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBook();
                    break;
                default:
                    System.out.println("无效操作！");
            }
        }else if(realUser instanceof NormalUser) {
            switch (choice) {
                case SEARCH_BOOK:
                    Library.getInstance().searchBook();
                    break;
                case DISPLAY_BOOK:
                    Library.getInstance().displayBook();
                    break;
                case EXIT:
                    Library.getInstance().exitBookSystem();
                    break;
                case BORROW_COUNT:
                    borrowBook();;
                    break;
                case RETURN_BOOK:
                    returnBook();
                    break;
                case VIEW_BORROW_BOOK:
                    viewBorrowBook();
                    break;
                default:
                    System.out.println("无效操作！");
            }
        }
    }
}
